package com.ruoyi.biz.controller;

import com.ruoyi.biz.dto.LotteryLogDTO;
import com.ruoyi.biz.dto.LotteryPrizeDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryPrizeDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.service.LotteryLogService;
import com.ruoyi.biz.service.LotteryPrizeService;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/biz/lotteryResult")
public class LotteryResultController extends BaseController {

    private String prefix = "lotteryResult";

    @Resource
    LotteryResultService lotteryResultService;
    @Resource
    LotteryPrizeService lotteryPrizeService;
    @Resource
    LotteryLogService lotteryLogService;

    @GetMapping("/{lotteryId}")
    public String build(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryId);
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LotteryResultDTO lotteryResultDTO) {
        startPage();
        List<LotteryResultDO> list = lotteryResultService.queryList(lotteryResultDTO);
        return getDataTable(list);
    }


    @GetMapping("/add/{lotteryId}")
    public String add(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryId);
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryId(lotteryId);
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        mmap.put("lotteryPrizeDTOS", lotteryPrizeDTOS);
        return prefix + "/add";
    }

    @Log(title = "中奖结果管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LotteryResultDTO lotteryResultDTO) {
        if (ValidateUtil.isEmpty(lotteryResultDTO)) {
            return error("新增的中奖结果信息入参为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getLotteryId())) {
            return error("新增的中奖结果信息入参中，抽奖活动ID为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getLotteryPrizeId())) {
            return error("新增的中奖结果信息入参中，奖品信息为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getTel())) {
            return error("新增的中奖结果信息入参中，中奖人联系电话为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getPsnName())) {
            return error("新增的中奖结果信息入参，中奖人姓名为空！");
        }
        //查询奖品信息
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        qLotteryPrizeDTO.setLotteryPrizeId(lotteryResultDTO.getLotteryPrizeId());
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        if (ValidateUtil.isEmpty(lotteryPrizeDTOS)) {
            return error("系统出错：查询奖项信息为空！");
        } else {
            //填充奖品信息
            lotteryResultDTO.setLotteryPrizeName(lotteryPrizeDTOS.get(0).getLotteryPrizeName());
            lotteryResultDTO.setLotteryPrizeTitle(lotteryPrizeDTOS.get(0).getLotteryPrizeTitle());
        }

        //判断中奖结果
        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setValiFlag("1");
        qLotteryResultDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);
        if (!ValidateUtil.isEmpty(lotteryResultDTOS)) {

            // 如果入参里的人已经中过奖，那就不让再中
            List<LotteryResultDO> filterList1 = lotteryResultDTOS.stream()
                    .filter(item -> lotteryResultDTO.getTel().equals(item.getTel()) && lotteryResultDTO.getPsnName().equals(item.getPsnName())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(filterList1)) {
                return error("联系人姓名为【" + lotteryResultDTO.getPsnName() +  "】，电话为【" + lotteryResultDTO.getTel() + "】已经中奖，不能重复中奖！");
            }

            // 当前活动的每项奖品的数量
            int prizeCount = lotteryPrizeDTOS.get(0).getPrizeCnt();
            // 当前活动的每项奖品的中奖数量
            Map<String, List<LotteryResultDO>> lotteryResultType = lotteryResultDTOS.stream().collect(Collectors.groupingBy(LotteryResultDO::getLotteryPrizeId));
            int resultCount = ValidateUtil.isEmpty(lotteryResultType.get(lotteryResultDTO.getLotteryPrizeId())) ? 0 : lotteryResultType.get(lotteryResultDTO.getLotteryPrizeId()).size();

            if (resultCount >= prizeCount) {
                return error(lotteryResultDTO.getLotteryPrizeTitle() + "名额已满，不能添加！");
            }
        }

        //中奖方式：1.系统添加
        lotteryResultDTO.setLotteryResultType("1");
        //组装基础数据
        lotteryResultDTO.setCrterId(getSysUser().getUserId().toString());
        lotteryResultDTO.setCrterName(getSysUser().getUserName());
        lotteryResultDTO.setCrteTime(new Date());
        lotteryResultDTO.setOpterId(getSysUser().getUserId().toString());
        lotteryResultDTO.setOpterName(getSysUser().getUserName());
        lotteryResultDTO.setOptTime(new Date());
        return toAjax(lotteryResultService.insertLotteryResult(lotteryResultDTO));
    }

    @Log(title = "中奖结果管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setLotteryResultId(ids);
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);
        if (ValidateUtil.isEmpty(lotteryResultDTOS)) {
            return error("系统出错，查询抽奖结果为空!");
        }
        LotteryLogDTO qLotteryLogDTO = new LotteryLogDTO();
        qLotteryLogDTO.setLotteryId(lotteryResultDTOS.get(0).getLotteryId());
        qLotteryLogDTO.setTel(lotteryResultDTOS.get(0).getTel());
        qLotteryLogDTO.setPsnName(lotteryResultDTOS.get(0).getPsnName());
        qLotteryLogDTO.setLotteryResultId(ids);
        qLotteryLogDTO.setValiFlag("1");
        List<LotteryLogDO> lotteryLogDTOS = lotteryLogService.queryList(qLotteryLogDTO);
        if (!ValidateUtil.isEmpty(lotteryLogDTOS)) {
//            return error("联系人姓名为【" + lotteryResultDTOS.get(0).getPsnName() +  "】，电话为【" + lotteryResultDTOS.get(0).getTel() + "】已经参与抽奖，不能进行删除！");
            return toAjax(lotteryResultService.deleteLotteryResult(ids, lotteryLogDTOS.get(0)));
        }
        return toAjax(lotteryResultService.deleteLotteryResult(ids));
    }
}
